//
//  TestController.m
//  AnimatedFlashcards
//
//  Created by Timothy Costa on 10/13/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#include <math.h>
#import "TestController.h"

#define QUEUE_SIZE 20
#define QUEUE_DONT_KNOW_POSITION 5
#define QUEUE_SO_SO_POSITION 10
#define SCORE_DONT_KNOW 0.0f
#define SCORE_SO_SO .5f
#define SCORE_NO_PROBLEM 1.0f

@implementation TestController

@synthesize isTesting;
@synthesize allowClicks;
@synthesize side1;
@synthesize side2;
@synthesize hint;
@synthesize questionQueue;
@synthesize questionAnswerButton;
@synthesize hintButton;
@synthesize side1Image;
@synthesize side2Image;

-(void)awakeFromNib
{
	allowClicks = YES;
	questionQueue = [[NSMutableArray alloc]initWithCapacity:0];
	currentSide = 1;
	initialSide = currentSide;
	alwaysShowHints = NO;
	side1Image = [NSImage alloc];
	side2Image = [NSImage alloc];
}
-(IBAction) openEditCardSheet:(id)sender
{	
	Card * selectedCard = [[deck_cards selectedObjects]objectAtIndex:0];
	[editCardController openEditCardController:selectedCard];
}
- (void) dontKnow: (id)sender
{
	[self scorerClicked: SCORE_DONT_KNOW];
}
- (IBAction) soSo: (id)sender
{
	[self scorerClicked: SCORE_SO_SO];
}
- (IBAction) noProblem: (id)sender
{
	[self scorerClicked: SCORE_NO_PROBLEM];
}
- (IBAction) toggleSide: (id)sender
{
	currentSide = [sender state] == NSOnState ? 2 : 1;
}
- (IBAction) testSideClicked: (id)sender
{
	[menuSide1 setState:NSOffState];
	[menuSide2 setState:NSOffState];
	[menuMaintainSelection setState:NSOffState];
	[sender setState:NSOnState];
	initialSide = [sender tag];
}
- (IBAction) hintClicked: (id)sender
{
	alwaysShowHints = !alwaysShowHints;
	[sender setState: alwaysShowHints ? NSOnState : NSOffState];
}

-(void)startTest:(NSMutableArray*)cardsForTest
{
	isTesting = YES;
	// Use the given cards, or pick random ones
	if (cardsForTest !=nil){
		[questionQueue setArray:cardsForTest];
	} else {
		[questionQueue setArray:[[NSMutableArray alloc]init]];
		// pick n random cards
		int count = [[deck_cards arrangedObjects] count];
		for (int i = 0; i<count && i<QUEUE_SIZE; i++){
			[questionQueue insertObject:[self randomCard] atIndex:0];
		}
	}

	[self showCard: [questionQueue objectAtIndex:0]];
}

- (void) scorerClicked: (float) score
{
	Card * selectedCard = [questionQueue objectAtIndex:0];
	[questionQueue removeObjectAtIndex:0];
	Results * selectedResults = selectedCard.results;
	[selectedResults updateScore: score];
	
	int questionCount = [questionQueue count];
	if (score==SCORE_DONT_KNOW){
		[questionQueue insertObject:selectedCard atIndex: 
						(questionCount > QUEUE_DONT_KNOW_POSITION ? QUEUE_DONT_KNOW_POSITION : questionCount)
		 ];
	} else if (score == SCORE_SO_SO){
		[questionQueue insertObject:selectedCard atIndex:
						(questionCount > QUEUE_SO_SO_POSITION ? QUEUE_SO_SO_POSITION : questionCount)
		];
	} else if (score == SCORE_NO_PROBLEM){
		Card * newCard = [self randomCard];
		if (newCard!=nil) [questionQueue insertObject:[self randomCard] atIndex:questionCount];
	}
	
	[self showCard: [questionQueue objectAtIndex:0]];
}
-(Card*) randomCard
{
	int randomNumber;
	Card * theRandomCard = nil;
	
	// choose only cards that we haven't added yet
	NSArray * cards = [deck_cards arrangedObjects];
	int n = 0;
	int cardCount = [cards count];
	
	while (n < cardCount*4){
		randomNumber = random()%cardCount;
		theRandomCard = [cards objectAtIndex:randomNumber];
		if (![questionQueue containsObject:theRandomCard])
			return theRandomCard;
		n++;
	}
	// Could not find a card in four tries above, so select the first card we can find.
	for (theRandomCard in cards){
		if (![questionQueue containsObject:theRandomCard])
			return theRandomCard;
	}
	// this shouldn't happen
	NSLog(@"Error: Could not find any card to add to questionQueue");
	return nil;
}
-(void) showCard: (Card*)cardToShow
{
	[deck_cards setSelectedObjects:[NSArray arrayWithObject:cardToShow]]; // Show this card via bindings

	NSString * trimmedHint = [cardToShow.hint stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	if (cardToShow.hint == nil || [trimmedHint isEqualToString: @""]){
		[hintButton setState: NSOffState];
		[[hintButton animator] setEnabled: NO];
	} else {
		[[hintButton animator] setEnabled: YES];
		[hintButton setState: alwaysShowHints ? NSOnState : NSOffState];
	}
		
	// show a side based on the user's preferences
	switch (initialSide){
		case 1:
			[questionAnswerButton setState:NSOffState];
			break;
		case 2:
			[questionAnswerButton setState:NSOnState];
			break;
		case 3: // don't change a thing
			break;
	}
}

@end
